package awt;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame {
	JPanel panel;
	JTextField textField;
	private double v1, v2, value;
	private char operator;
	private boolean reop = false;

	public Calculator() {
		this.setTitle("计算器");
		this.setSize(240, 320);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(null);
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(0, 0, 240, 60);
		textField.setHorizontalAlignment(JTextField.RIGHT);
		this.add(textField);
		panel = new JPanel();
		panel.setLayout(new GridLayout(4, 4));
		panel.setBounds(0, 60, 240, 240);
		panel.add(useButton('7'));
		panel.add(useButton('8'));
		panel.add(useButton('9'));
		panel.add(useButton('+'));
		panel.add(useButton('4'));
		panel.add(useButton('5'));
		panel.add(useButton('6'));
		panel.add(useButton('-'));
		panel.add(useButton('1'));
		panel.add(useButton('2'));
		panel.add(useButton('3'));
		panel.add(useButton('×'));
		panel.add(useButton('0'));
		panel.add(useButton('.'));
		panel.add(useButton('='));
		panel.add(useButton('÷'));
		this.add(panel);
		this.setVisible(true);
	}

	public static void main(String[] args) {
		Calculator cal = new Calculator();
	}

	public JButton useButton(final char key) {
		JButton button = new JButton(String.valueOf(key));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				JButton btn = (JButton) event.getSource();
				char key2 = btn.getText().charAt(0);
				action(key2);
			}
		});
		button.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent event) {
				char key2 = event.getKeyChar();

				// if(key2 != key)
				// return;

				action(key2);
			}
		});

		return button;
	}

	private void action(char key2) {
		if (reop) {
			textField.setText("");
			reop = false;
		}

		switch (key2) {
		case '+':
			v1 = Double.parseDouble(textField.getText());
			textField.setText(textField.getText() + "+");
			operator = '+';
			break;
		case '-':
			v1 = Double.parseDouble(textField.getText());
			textField.setText(textField.getText() + "-");
			operator = '-';
			break;
		case '×':
			v1 = Double.parseDouble(textField.getText());
			textField.setText(textField.getText() + "×");
			operator = '*';
			break;
		case '÷':
			v1 = Double.parseDouble(textField.getText());
			textField.setText(textField.getText() + "÷");
			operator = '/';
			break;
		case '=':
			reop = true;
			String vv = textField.getText();
			int index1 = vv.indexOf("+");
			int index2 = vv.indexOf("-");
			int index3 = vv.indexOf("×");
			int index4 = vv.indexOf("÷");
			int index = index1 == -1 ? index2 == -1 ? index3 == -1 ? index4 : index3 : index2 : index1;
			v2 = Double.parseDouble(vv.substring(index + 1));
			switch (operator) {
			case '+':
				value = v1 + v2;
				break;
			case '-':
				value = v1 - v2;
				break;
			case '*':
				value = v1 * v2;
				break;
			case '/':
				value = v1 / v2;
				break;
			default:
				;
			}
			textField.setText(String.valueOf(value));
			break;
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '0':
		case '.':
			String text = textField.getText() + key2;
			textField.setText(text);
			break;
		default:
			;
		}
	}

}
